package com.zhj.algorithm.search.str;

public class BFSearch implements StrSearch {

    @Override
    public int search(String A, String B) {
        if (A == null || A.length() == 0 || B == null || B.length() == 0) {
            return -1;
        }
        for (int i = 0; i < A.length(); i++) {
            if (i + B.length() > A.length()) {
                break;
            }
            if (A.charAt(i) == B.charAt(0)) {
                boolean flag = true;
                for (int j = 0; j < B.length(); j++) {
                    if (B.charAt(j) != A.charAt(i + j)) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    return i+1;
                }
            }
        }
        return -1;
    }
}
